﻿/* Copyright (c) 2009, 2010, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class x86_64_Assembler
    {
        IEnumerable<OutputBlock> x86_64_conv_i4u2zx_gpr_gprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, 0x0f, 0xb7)); }

        IEnumerable<OutputBlock> x86_64_conv_i48_i2sx_dgpr_sgprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, (op == ThreeAddressCode.Op.conv_i4_i2sx) ? false : true, 0, 0x0f, 0xbf)); }

        IEnumerable<OutputBlock> x86_64_conv_i48_i1sx_dgpr_sgprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        { return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, (op == ThreeAddressCode.Op.conv_i4_i2sx) ? false : true, 0, 0x0f, 0xbe)); }

        IEnumerable<OutputBlock> x86_64_conv_i4u1zx_gpr_gprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, false, 0, true, 0x0f, 0xb6));
        }

        IEnumerable<OutputBlock> x86_64_conv_i4i8sx_gpr_gprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            return OBList(EncOpcode(result.hardware_loc, op1.hardware_loc, 3, true, 0, 0x63));
        }

        IEnumerable<OutputBlock> x86_64_conv_i4u8zx_sgprmem_dgprmem(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            /* There is no 32 to 64 bit movzx instruction in x86_64
             * 
             * We have to load a value 0x0000 0000 ffff ffff to a temporary register,
             * 'AND' the source with it then write to dest
             * We could optimise this to 2 instructions in most circumstances but for now
             * keep it simple */

            List<byte> ret = new List<byte>();

            // Load mask value to a temporary register
            x86_64_gpr temp_reg = state.reg_alloc.GetTemporaryRegister() as x86_64_gpr;
            ret.AddRange(EncAddOpcode(temp_reg, true, 0xb8, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00));

            // Do AND
            ret.AddRange(EncOpcode(temp_reg, op1.hardware_loc, 3, true, 0, 0x23));

            // Store result
            ret.AddRange(EncOpcode(temp_reg, result.hardware_loc, 3, true, 0, 0x89));

            return OBList(ret);
        }
    }
}
